package org.dromara.todolist.service.impl;

import java.util.Map;

import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.todolist.domain.TdDemo;
import org.dromara.todolist.domain.bo.TdDemoBo;
import org.dromara.todolist.domain.vo.TdDemoVo;
import org.dromara.todolist.mapper.TdDemoMapper;
import org.dromara.todolist.service.ITdDemoService;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;

import jakarta.annotation.Resource;

/**
 * 测试单表 td_demo Service业务层处理
 *
 * @author jy.chen
 */
@Service
public class TdDemoServiceImpl implements ITdDemoService {

    @Resource
    private TdDemoMapper baseMapper;

    @Override
    public TableDataInfo<TdDemoVo> queryPageList(TdDemoBo tdDemoBo, PageQuery pageQuery) {
        LambdaQueryWrapper<TdDemo> lqw = buildQueryWrapper(tdDemoBo);
        Page<TdDemoVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    private LambdaQueryWrapper<TdDemo> buildQueryWrapper(TdDemoBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<TdDemo> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getTestKey()), TdDemo::getTestKey, bo.getTestKey());
        lqw.eq(StringUtils.isNotBlank(bo.getValue()), TdDemo::getValue, bo.getValue());
        lqw.between(params.get("beginCreateTime") != null && params.get("endCreateTime") != null,
            TdDemo::getCreateTime, params.get("beginCreateTime"), params.get("endCreateTime"));
        lqw.orderByAsc(TdDemo::getId);
        return lqw;
    }
}
